package utils;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.time.Duration;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidTouchAction;
import io.appium.java_client.touch.LongPressOptions;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.ElementOption;
import io.appium.java_client.touch.offset.PointOption;

public class BaseAction {
	public AndroidDriver<AndroidElement> driver;
	public AndroidTouchAction action;

	public BaseAction(AndroidDriver<AndroidElement> driver) {
		this.driver = driver;
		action = new AndroidTouchAction(driver);

	}

	public String getToastMsg() {
		return this.driver.findElementByXPath("//*[@class='android.widget.Toast']").getText();
	}

	/**
	 * @param by
	 */
	public void click(By by) {

		driver.findElement(by).click();
	}

	public void click(WebElement elemnet) throws InterruptedException {
		elemnet.click();
		Thread.sleep(3000);
	}

	public void type(WebElement element, String context) throws InterruptedException {
		element.sendKeys(context);
		Thread.sleep(3000);

	}

	public void name() {

	}

	// 长按
	public void longPress(WebElement ele) throws InterruptedException {
		LongPressOptions longPressOptions = LongPressOptions.longPressOptions();
		longPressOptions.withElement(ElementOption.element(ele));

		action.longPress(longPressOptions).perform();
		Thread.sleep(3000);
	}

	// 坐标点的滑动
	public void swip(PointOption fromOption, PointOption toOption) {
		action.press(fromOption).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1))).moveTo(toOption)
				.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1))).release().perform();
	}

	// 元素间的滑动
	public void swip(WebElement fromEle, WebElement toEle) {

		PointOption fromOption = PointOption.point(fromEle.getLocation().getX(), fromEle.getLocation().getY());
		PointOption toOption = PointOption.point(toEle.getLocation().getX(), toEle.getLocation().getY());
		swip(fromOption, toOption);
	}
	// 拖动

	public void longClickAndSwip(AndroidElement element) {

		LongPressOptions longPressOptions = LongPressOptions.longPressOptions();
		longPressOptions.withElement(ElementOption.element(element));

		action.longPress(longPressOptions).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
				.moveTo(PointOption.point(100, element.getLocation().getY()))
				.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1))).release().perform();
	}

	public void setValue(By by, String value) {
		driver.findElement(by).clear();
		driver.findElement(by).sendKeys(value);

	}

}
